import { Injectable, InternalServerErrorException } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { AdminGetAddressListDto, AdminGetUserListDto } from "./dto/user.dto";
import { Address } from "/@/entities/user/Address.entity";
import { User } from "/@/entities/user/User.entity";
import { format } from "/@/utils/moment";
@Injectable()
export class AdminUserService {
  constructor(
    private readonly configService: ConfigService,
    @InjectRepository(User)
    private userRepository: Repository<User>,
    @InjectRepository(Address)
    private addressRepository: Repository<Address>,
  ) {
    //
  }
  /**
   * 获取用户列表
   * @param params 字段
   */
  async getUserList(query: AdminGetUserListDto) {
    const { pageSize = 20, page = 1, nickName = "", id } = query;
    try {
      let qb = this.userRepository.createQueryBuilder("user");
      qb = qb
        .skip(pageSize * (page - 1))
        .take(pageSize)
        .where("user.nickName like :nickName", {
          nickName: "%" + nickName + "%",
        });
      if (id) {
        qb = qb.andWhere("user.id = :id", { id });
      }
      const data = await qb.getManyAndCount();
      const items = data[0].map((item: any) => {
        item.avatarUrl = item.avatarUrl ? item.avatarUrl.split(",") : [];
        // item.openid = "****" + item.openid.slice(0, 8);
        item.create_at = format(item.create_at, "YYYY-MM-DD HH:mm:ss");
        return item;
      });
      return { items, total: data[1] };
    } catch (error) {
      throw new InternalServerErrorException("查询失败");
    }
  }

  /**
   * 获取用户地址列表
   * @param params 字段
   */
  async getAddressList(query: AdminGetAddressListDto) {
    const { pageSize = 20, page = 1, user_id, user_name, user_phone } = query;
    try {
      let qb = this.addressRepository.createQueryBuilder("address");
      qb = qb.limit(pageSize).offset((page - 1) * pageSize);

      if (user_id) {
        qb = qb.andWhere("user_id = :user_id", { user_id });
      }
      if (user_name) {
        qb = qb.andWhere("name = :name", { name: user_name });
      }
      if (user_phone) {
        qb = qb.andWhere("phone = :phone", { phone: user_phone });
      }

      qb = qb
        .leftJoinAndSelect(User, "user", "user.id = address.user_id")
        .select(
          `address.id as id,
          address.user_id as user_id,
          user.nickName as nickName,
          user.avatarUrl as avatarUrl,
          address.name as name,
          address.phone as phone,
          address.province as province,
          address.city as city,
          address.country as country,
          address.detailed as detailed,
          address.update_at as update_at`,
        );
      const items = await qb.getRawMany();
      items.map((item) => {
        item.avatarUrl = item.avatarUrl ? item.avatarUrl.split(",") : [];
        item.update_at = format(item.update_at, "YYYY-MM-DD HH:mm:ss");
        return item;
      });
      const total = await qb.getCount();
      return { items, total };
      // let qb = this.addressRepository.createQueryBuilder("address");
      // qb = qb
      //   .skip(pageSize * (page - 1))
      //   .take(pageSize)
      //   .orderBy("address.update_at", "DESC");
      // if (user_id) {
      //   qb = qb.andWhere("address.user_id = :user_id", { user_id });
      // }
      // if (id) {
      //   qb = qb.andWhere("address.id = :id", { id });
      // }
      // const [items, total] = await qb.getManyAndCount();
      // return { items, total };
    } catch (error) {
      console.log(error);
      throw new InternalServerErrorException("查询失败");
    }
  }
}
